package com.xt.purchasingsystem.service.impl.productionManagementServiceImpl;

import com.xt.purchasingsystem.dto.MDesignProcedureDetailsDto;
import com.xt.purchasingsystem.dto.MDesignProcedureDto;
import com.xt.purchasingsystem.mapper.DModuleDetailsMapper;
import com.xt.purchasingsystem.mapper.MDesignProcedureDetailsMapper;
import com.xt.purchasingsystem.mapper.MDesignProcedureMapper;
import com.xt.purchasingsystem.mapper.MDesignProcedureModuleMapper;
import com.xt.purchasingsystem.model.DModuleDetails;
import com.xt.purchasingsystem.model.MDesignProcedure;
import com.xt.purchasingsystem.model.MDesignProcedureDetails;
import com.xt.purchasingsystem.model.MDesignProcedureModule;
import com.xt.purchasingsystem.service.productionManagementService.ProcessMaterialDesignService;
import com.xt.purchasingsystem.utils.PageBean;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import com.xt.purchasingsystem.vo.DModuleDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 生产管理--工序物料设计业务实现类
 * Author: 老姚
 * Date:  2020/3/17 21:33
 */
@Service
@Transactional

public class ProcessMaterialDesignServiceImpl implements ProcessMaterialDesignService {
    @Autowired
    private MDesignProcedureMapper mDesignProcedureMapper;
    @Autowired
    private MDesignProcedureDetailsMapper mDesignProcedureDetailsMapper;
    @Autowired
    private DModuleDetailsMapper dModuleDetailsMapper;
    @Autowired
    private MDesignProcedureModuleMapper mDesignProcedureModuleMapper;

    @Override
    public Result queryAllapprovedMDesignProcedureByPaging(MDesignProcedureDto mDesignProcedureDto) {
        int rowCount = mDesignProcedureMapper.rowCountAllapprovedMDesignProcedure();
        PageBean pageBean = new PageBean(mDesignProcedureDto.getPageSize(), mDesignProcedureDto.getCurrNo(), rowCount);
        pageBean.setCurrNo(pageBean.getStart());
        pageBean.setPageSize(pageBean.getEnd());
        List<MDesignProcedureDto> mDesignProcedureDtos = mDesignProcedureMapper.queryAllapprovedMDesignProcedureByPaging(mDesignProcedureDto);
        pageBean.setPageList(mDesignProcedureDtos);
        return ResultFactory.buildSuccessResult("查询成功", pageBean);
    }

    @Override
    public Result queryAllDModuleDetailsByProductId(String productId, Integer currNo, Integer pageSize) {
        int rowCount = dModuleDetailsMapper.rowCountByByProductId(productId);
        PageBean pageBean = new PageBean(pageSize, currNo, rowCount);
        List<DModuleDetailsVo> dModuleDetails = dModuleDetailsMapper.queryAllDModuleDetailsByProductId(productId, pageBean.getStart(), pageBean.getEnd());
        pageBean.setPageList(dModuleDetails);
        return ResultFactory.buildSuccessResult("查询成功", pageBean);
    }

    @Override
    public Result getAllMDesignProcedureModuleByProcudureId(Integer procudureId) {
        List<MDesignProcedureModule> allByProcudureId = mDesignProcedureModuleMapper.getAllByProcudureId(procudureId);
        return ResultFactory.buildSuccessResult("查询成功", allByProcudureId);

    }

    @Override
    public Result designMaterial(MDesignProcedureDetailsDto mDesignProcedureDetailsDto) {
        MDesignProcedure mdesignProcedure = mDesignProcedureDetailsDto.getMdesignProcedure();
        System.out.println(mdesignProcedure);
        MDesignProcedureDetails mdesignProcedureDetail = mDesignProcedureDetailsDto.getMdesignProcedureDetail();
        List<MDesignProcedureModule> mdesignProcedureModules = mDesignProcedureDetailsDto.getMdesignProcedureModules();
        mDesignProcedureDetailsMapper.updateDesignModuleTag(mdesignProcedureDetail.getId(), "D002-1");
        Double sum = 0.0;
        if (mdesignProcedureModules.size() > 0) {
            for (int i = 0; i < mdesignProcedureModules.size(); i++) {
                sum += mdesignProcedureModules.get(i).getSubtotal().doubleValue();
                //根据编号减少物料可用数量
                dModuleDetailsMapper.reduceResidualAmount(mdesignProcedureModules.get(i).getId(), mdesignProcedureModules.get(i).getAmount().intValue());
                mdesignProcedureModules.get(i).setDetailsNumber(i + 1);
                //添加产品生产工序物料明细
                mDesignProcedureModuleMapper.insert(mdesignProcedureModules.get(i));
            }
            mDesignProcedureDetailsMapper.updateModuleSubtotal(mdesignProcedureDetail.getId(), sum);

        }

        return ResultFactory.buildSuccessResult("工序物料设计成功");

    }

    @Override
    public Result redesign(MDesignProcedureDetailsDto mDesignProcedureDetailsDto) {
        MDesignProcedure mdesignProcedure = mDesignProcedureDetailsDto.getMdesignProcedure();
        MDesignProcedureDetails mdesignProcedureDetail = mDesignProcedureDetailsDto.getMdesignProcedureDetail();
        List<MDesignProcedureModule> mdesignProcedureModules = mDesignProcedureDetailsDto.getMdesignProcedureModules();
        mDesignProcedureDetailsMapper.updateDesignModuleTag(mdesignProcedureDetail.getId(), "D002-0");

        if (mdesignProcedureModules.size() > 0) {
            List<DModuleDetails> dModuleDetails = dModuleDetailsMapper.queryAllByProductId(mdesignProcedure.getProductId());
            for (DModuleDetails dModuleDetail : dModuleDetails) {
                for (MDesignProcedureModule mdesignProcedureModule : mdesignProcedureModules) {
                    if (mdesignProcedureModule.getProductId().equals(dModuleDetail.getProductId()) &&
                            mdesignProcedureModule.getProductName().equals(dModuleDetail.getProductName())
                    ) {
                        dModuleDetailsMapper.saveResidualAmount(dModuleDetail.getId(), mdesignProcedureModule.getAmount().intValue());
                    }
                }
            }
            System.out.println(mdesignProcedureDetail.getId());
            mDesignProcedureDetailsMapper.updateModuleSubtotal(mdesignProcedureDetail.getId(), 0.0);

            mDesignProcedureModuleMapper.deleteAllMDesignProcedureModuleByDesignProcedureDetailsId(mdesignProcedureModules.get(0).getParentId());
        }

        return ResultFactory.buildSuccessResult("工序物料已重新设计");

    }

    @Override
    public Result submitProcessMaterialDesignSheet(Integer id, String productId) {
        int i = mDesignProcedureMapper.updateDesignModuleTag(id, "G002-1");
        if (i == 1) {
            List<DModuleDetails> dModuleDetails = dModuleDetailsMapper.queryAllByProductId(productId);
            Double sum = 0.0;
            if (dModuleDetails.size() > 0) {
                for (DModuleDetails dModuleDetail : dModuleDetails) {
                    sum += dModuleDetail.getSubtotal().doubleValue();
                }
            }
            int result = mDesignProcedureMapper.updateModuleCostPriceSumById(id, sum);
            if (result == 1) {
                return ResultFactory.buildSuccessResult("工序物料设计单提交成功,需要审核");
            } else {
                return ResultFactory.buildFailResult("工序物料设计单提交失败");
            }
        } else {
            return ResultFactory.buildFailResult("工序物料设计单提交失败");
        }
    }

    @Override
    public Result checkMaterialResidualAmount(String productId) {
        List<DModuleDetails> dModuleDetails = dModuleDetailsMapper.queryAllByProductId(productId);
        boolean flag = true;
        for (DModuleDetails dModuleDetail : dModuleDetails) {
            if (dModuleDetail.getResidualAmount().intValue() > 0) {
                flag = false;
            } else {
                flag = true;
            }
        }
        return ResultFactory.buildSuccessResult("查询成功", flag);
    }

    @Override
    public Result queryAllMDesignProcedureModuleBycheckedAndMaterialDesigned(MDesignProcedureDto mDesignProcedureDto) {
        int rowCount = mDesignProcedureMapper.rowCountAllcheckedAndMaterialDesigned();
        PageBean pageBean = new PageBean(mDesignProcedureDto.getPageSize(), mDesignProcedureDto.getCurrNo(), rowCount);
        pageBean.setCurrNo(pageBean.getStart());
        pageBean.setPageSize(pageBean.getEnd());
        List<MDesignProcedureDto> mDesignProcedureDtos = mDesignProcedureMapper.queryAllcheckedAndMaterialDesigned(mDesignProcedureDto);
        pageBean.setPageList(mDesignProcedureDtos);
        return ResultFactory.buildSuccessResult("查询成功", pageBean);
    }

    @Override
    public Result reviewProcessMaterialDesignList(MDesignProcedure mdesignProcedure) {
        int i = mDesignProcedureMapper.updateByPrimaryKeySelective(mdesignProcedure);
        if (i==1){
            return ResultFactory.buildSuccessResult("审核成功");

        }else {
            return ResultFactory.buildFailResult("审核失败");
        }
    }
}
